package edu.thu.studio.ormdesigner.editor.figure.connection;

import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MidpointLocator;
import org.eclipse.draw2d.PolylineConnection;

import edu.thu.studio.ormdesigner.model.diagram.link.Link;
import edu.thu.studio.ormdesigner.model.diagram.node.TableNode;
import edu.thu.studio.ormdesigner.model.orm.table.Reference;
import edu.thu.studio.ormdesigner.model.orm.table.Table;
import edu.thu.studio.ormdesigner.util.Utils;

public class LineConnection extends PolylineConnection {

	private Label nameLabel;
	private Link link;

	public LineConnection(Link link) {
		this.link=link;
		this.nameLabel = new Label();

		nameLabel.setOpaque(true);

		this.add(nameLabel, new MidpointLocator(this, 0));

	}

	public void refreshLabel() {
		nameLabel.setText(getLabelText());
	}
	
	private String getLabelText(){

		TableNode fromTableNode = (TableNode) link.getSource();
		Table fromTable = fromTableNode.getRefTable();
		Reference reference = fromTable.getReferences().getReferenceById(
				link.getReferenceId());

		if (null != reference) {
			if (!Utils.isNull(reference.getName())) {
				return reference.getName();
			}
		}
		return "";
	}

}
